﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Util;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;

namespace SalesManagement.Modules.Warehouse
{
    public partial class StockTransferList : BasePage
    {
        public StockTransferList()
            : base(SCREENS.STOCKTRANFERLIST)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [DirectMethod]
        public void DocumentReady()
        {
            LoadWarehouse();
            LoadEmployee();
        }
        /// <summary>
        /// Load Kho hàng
        /// </summary>
        [DirectMethod]
        public void LoadWarehouse()
        {

            WareHouseService warehouseS = WareHouseService.GetInstance();
            IList<WareHouseEntity> lstWarehouse = warehouseS.LoadAll();
            if (null != lstWarehouse)
            {
                WareHouseEntity warehouseE = new WareHouseEntity();
                warehouseE.WarehouseId = 0;
                warehouseE.WarehouseName = "Tất cả";
                lstWarehouse.Insert(0, warehouseE);
                this.storeWarehouse.DataSource = lstWarehouse;
                this.storeWarehouse.DataBind();
            }
        }
        /// <summary>
        /// Delete Voucher select
        /// </summary>
        /// <param name="VoucherID"></param>
        /// <returns></returns>
        [DirectMethod]
        public bool DeleteProcess(long VoucherID)
        {
            VoucherService voucherS = VoucherService.GetInstance();
            VoucherDetailService voucherDetailS = VoucherDetailService.GetInstance();
            if (!voucherDetailS.DeleteVoucherDetailByVoucherId(VoucherID)) return false;
            if (!voucherS.Delete(VoucherID)) return false;
            return true;
        }
        /// <summary>
        /// Load nhan vien
        /// </summary>
        [DirectMethod]
        protected void LoadEmployee()
        {

            EmployeeService employeeS = EmployeeService.GetInstance();
            IList<EmployeeEntity> lstEmployee = employeeS.LoadAll();
            if (null != lstEmployee)
            {
                EmployeeEntity employeeE = new EmployeeEntity();
                employeeE.EmployeeId = 0;
                employeeE.EmployeeName = "Tất cả";
                lstEmployee.Insert(0, employeeE);
                this.storeEmployee.DataSource = lstEmployee;
                this.storeEmployee.DataBind();
            }
        }
        protected void VoucherList_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            int start = int.Parse(e.Parameters["start"]);
            int limit = int.Parse(e.Parameters["limit"]);
            limit += start;

            VoucherEntity voucherE = new VoucherEntity();
            if (!string.IsNullOrEmpty(txtVoucherNo.Text))
                voucherE.VoucherNo = txtVoucherNo.Text;
            if (txtToDate.SelectedValue != null)
                voucherE.ToDate = txtToDate.SelectedDate;
            if (txtFromDate.SelectedValue != null)
                voucherE.FromDate = txtFromDate.SelectedDate;
            if (cboWarehouse.Value != null && !"0".Equals(cboWarehouse.Value))
                voucherE.WarehouseId = Util.Obj2Int(cboWarehouse.Value);
            if (cboToWarehouse.Value != null && !"0".Equals(cboWarehouse.Value))
                voucherE.ToWarehouseId = Util.Obj2Int(cboToWarehouse.Value);
            if (cboEmployee.Value != null && !"0".Equals(cboWarehouse.Value))
                voucherE.EmployeeId = Util.Obj2Int(cboEmployee.Value);


            VoucherService voucherS = VoucherService.GetInstance();
            voucherE.Start = start;
            voucherE.EndNum = limit;
            voucherE.TotalRow = 0;
            IList<VoucherEntity> Resul = voucherS.TranferStockSearchData(voucherE);
            e.Total = voucherE.TotalRow;
            stVoucherList.DataSource = Resul;
            stVoucherList.DataBind();
        }
        /// <summary>
        /// Search Danh sách phiếu chuyển kho
        /// </summary>
        [DirectMethod]
        public void SearchDocument(int Page)
        {


        }
        /// <summary>
        /// Get all voucher detail by voucher id
        /// </summary>
        /// <param name="MasterID"></param>
        [DirectMethod]
        public void SelectionChange(int MasterID)
        {
            VoucherDetailService service = VoucherDetailService.GetInstance();
            IList<VoucherDetailEntity> resul = service.GetAllDetailByMasterID(MasterID);
            if (resul != null)
                stVoucherDetail.DataSource = resul;
            stVoucherDetail.DataBind();
        }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e) { }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {

        }
        public void btnEdit_Click(object sender, DirectEventArgs e) { }
        public void btnDelete_Click(object sender, DirectEventArgs e) { }
        public void btnFind_Click(object sender, DirectEventArgs e) { }
    }
}
